package com.zeros.mysplashtwo.model;

import com.zeros.mysplashtwo.MyApplication;
import com.zeros.mysplashtwo.bean.AdMessageBean;
import com.zeros.mysplashtwo.bean.LoginCheckBean;
import com.zeros.mysplashtwo.http.API;
import com.zeros.mysplashtwo.http.RetrofitService;
import com.zeros.mysplashtwo.utils.Constants;
import com.zeros.mysplashtwo.utils.SPUtilsTwo;
import com.zeros.mysplashtwo.view.contract.LauncherContract;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import okhttp3.ResponseBody;

/**
 * Model实现类 on 2017/12/22.
 * 处理数据类(访问网络)
 */

public class LauncherModelImp implements LauncherContract.Model {
    private API api;

    public LauncherModelImp() {
        api = RetrofitService.getInstance();
    }

    //1.假装服务器要展示广告
    public Observable<LoginCheckBean> getLoginCheck() {
        return Observable.create(new ObservableOnSubscribe<LoginCheckBean>() {
            @Override
            public void subscribe(ObservableEmitter<LoginCheckBean> e) throws Exception {
                e.onNext(new LoginCheckBean(true));
                e.onComplete();
            }
        });
    }

    //假装要展示 3 秒广告，且广告图为如下图
    public Observable<AdMessageBean> getAdMessage() {
        return Observable.create(new ObservableOnSubscribe<AdMessageBean>() {
            @Override
            public void subscribe(ObservableEmitter<AdMessageBean> e) throws Exception {
                String imgUrl = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4142583680,4272693857&fm=27&gp=0.jpg";
                String imgUrlTo = "https://book.douban.com/series/660?page=1";

                SPUtilsTwo.put(MyApplication.getContext(), Constants.LAUNCHURL_KEY, imgUrlTo);
                e.onNext(new AdMessageBean(3, imgUrl, imgUrlTo));
                e.onComplete();
            }
        });
    }


    public Observable<ResponseBody> downLoadFile(String fileUrl) {
        return api.downLoadFile(fileUrl);
    }


}
